package com.ruoyix.generator.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.generator.domain.vo.GenModuleVo;
import com.ruoyix.generator.domain.bo.GenModuleBo;
import com.ruoyix.generator.service.IGenModuleService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * 模块管理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/generator/module")
public class GenModuleController extends BaseController {

    private final IGenModuleService iGenModuleService;

    /**
     * 查询模块管理列表
     */
    @SaCheckPermission("generator:module:list")
    @GetMapping("/list")
    public TableDataInfo<GenModuleVo> list(GenModuleBo bo, PageQuery pageQuery) {
        return iGenModuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出模块管理列表
     */
    @SaCheckPermission("generator:module:export")
    @Log(title = "模块管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GenModuleBo bo, HttpServletResponse response) {
        List<GenModuleVo> list = iGenModuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "模块管理", GenModuleVo.class, response);
    }

    /**
     * 获取模块管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("generator:module:query")
    @GetMapping("/{id}")
    public R<GenModuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iGenModuleService.queryById(id));
    }

    /**
     * 新增模块管理
     */
    @SaCheckPermission("generator:module:add")
    @Log(title = "模块管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GenModuleBo bo) {
        return toAjax(iGenModuleService.insertByBo(bo));
    }

    /**
     * 修改模块管理
     */
    @SaCheckPermission("generator:module:edit")
    @Log(title = "模块管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GenModuleBo bo) {
        return toAjax(iGenModuleService.updateByBo(bo));
    }

    /**
     * 删除模块管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("generator:module:remove")
    @Log(title = "模块管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iGenModuleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @SaCheckPermission("generator:module:add")
    @RequestMapping("/generateModule")
    public R<Void> generateModule(String path, String moduleCode,String moduleName){
        return  toAjax(iGenModuleService.generateModule(path, moduleCode, moduleName));
    }

}
